package com.example.examinationtest;

import java.util.Vector;

import com.example.examinationtest.MainActivity.TimerCounting;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.renderscript.Sampler.Value;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class CheckActivity extends Activity {
	static int i=0,pos=1;
	static int ipo=1;
	static int j=0,m=0;
	  String[] listQuestion;
	  String[] listAnswer,listCorrectAnswer,parts,correctParts;
	  CorrectAnswer[] Canswer;
	  Vector  checkCorrect=new Vector(),checkWrong =new Vector(),correctAnswer1=new Vector();
	  String answer,question,correctAnswer;
	  String position;
	  
	  int count=0,countWrong=0;
	  int result=0;
	  int toTalQuestion=0;
	  boolean correct=false;
	  Vector Status= new Vector();
	  boolean check =false;	        
	  int number=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check);
		listQuestion=getResources().getStringArray(R.array.Question);
		toTalQuestion=listQuestion.length;
		listAnswer=getResources().getStringArray(R.array.Answer);
		listCorrectAnswer=getResources().getStringArray(R.array.Result);
		final TextView txtShow=(TextView)findViewById(R.id.txtShow);
		final RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
		final RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
		final RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
		final RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
		  final Button btnPrevious=(Button)findViewById(R.id.Previous);
		 number=listQuestion.length; 
			
		 rd1.setEnabled(false);
		 rd2.setEnabled(false);
		 rd3.setEnabled(false);
		 rd4.setEnabled(false);
 		 btnPrevious.setEnabled(false);
 		 
 //		 rd2.setChecked(true);
		 
 	
 		 
 		 
        // rd1.setSelected(false);

		 Canswer=loadPosition();
		 loadQuestion(i);
			loadAnswer(i);
		int position=Integer.parseInt(Canswer[0].getPosition());
		  checkRight(position);
		  
			 final Button btnNext=(Button)findViewById(R.id.Next);
			 
			 btnNext.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					   btnPrevious.setEnabled(true);   		        
					     txtShow.setText("List :" + number +":"+ipo);
							
									
					     RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
						 RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
						 RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
						 RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
						 final Button btnNext=(Button)findViewById(R.id.Next);
					//TexView txtShow=(TextView)findViewById(R.id.textView2);
				 //   txtShow.setText("Result :"+result+"/"+toTalQuestion);
	          
						
					
					i=i+1;
					j=j+4;
					 
						
					
					
					// TODO Auto-generated method stub
	if((i>0 && i<listQuestion.length)&&(j>0 &&j<listAnswer.length))
	{
	loadQuestion(i);	
	loadAnswer(j);

	}
	int position=Integer.parseInt(Canswer[ipo].getPosition());
	  checkRight(position);
	  if((ipo==number-1))
		{
		btnNext.setEnabled(false);
			}

	ipo=ipo+1;


		
		

						
	//RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
	//rb.clearCheck();
			
				}
				
			});
		
			 {
					@Override
					public void onClick(View v) {
					// txtShow.setText("Result :"+result+"/"+toTalQuestion);
						//	check();
			         if(i>=1){
			        	 btnNext.setEnabled(true);
						String value1="";
						i=i-1;
						j=j-4;
						ipo=ipo-1;
						
						//check(ipo);
						//	txtShow.setText(i+"");
						
						
						// TODO Auto-generated method stub
			if((i>=0 && i<=listQuestion.length)&&(j>=0 &&j<=listAnswer.length))
			{
			loadQuestion(i);	
			loadAnswer(j);

			}

			//Status.removeAllElements();
			//RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
			//rb.clearCheck();
			check=true;
			         }

					
					}
				});

		
	
		
		
		
	}
	  CorrectAnswer[] loadPosition()
	  {   
		  
		  Canswer  = new CorrectAnswer[100];
			
			for(int i=0;i<listCorrectAnswer.length;i++)
	        {
				parts=listCorrectAnswer[i].split(" ");
				question=parts[0];
				answer=parts[1];
				position=parts[2];
				Canswer[i]=new CorrectAnswer();
	        	Canswer[i].setQuestion(question);
	        	Canswer[i].setAnswerCorrect(answer);
	        	Canswer[i].setPosition(position);
	        	
			
		
	   
	    		
	        }
		
		  
		  
		  return Canswer;	

	  }

	  
	void loadQuestion(int i)
	{   TextView content=(TextView)findViewById(R.id.txtQuestion);
	    
	    

		content.setText(listQuestion[i]);	

	}
void checkRight(int position)
{
                 RadioButton rd1=(RadioButton)findViewById(R.id.Answer1);
				 RadioButton rd2=(RadioButton)findViewById(R.id.Answer2);
				 RadioButton rd3=(RadioButton)findViewById(R.id.Answer3);
				 RadioButton rd4=(RadioButton)findViewById(R.id.Answer4);
	
	 switch (position) {
	    case 1:
	    {
	    	rd1.setChecked(true);
        	break;
	    }
	    case 2:
	    {
	    	rd2.setChecked(true);
        	break;
	    }
	    case 3:
	    {
	    	rd3.setChecked(true);
        	break;
	    }
	    case 4:
	    {
	    	rd4.setChecked(true);
        	break;
	    }
	 	
		 
	 }
	 }



	void loadAnswer(int j)
	{   

		
	final RadioButton Answer1=(RadioButton)findViewById(R.id.Answer1);
	final RadioButton Answer2=(RadioButton)findViewById(R.id.Answer2);
	final RadioButton Answer3=(RadioButton)findViewById(R.id.Answer3);
	final RadioButton Answer4=(RadioButton)findViewById(R.id.Answer4);
	RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
//			rb.clearCheck();
//		check();
	//if(j>=0 && j<listAnswer.length)
	{
		
	Answer1.setText(listAnswer[j]);	
	Answer2.setText(listAnswer[j+1]);
	Answer3.setText(listAnswer[j+2]);
	Answer4.setText(listAnswer[j+3]);	
	}
						

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.check, menu);
		return true;
	}

}
